package yaty.lee.tencent.weibo.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;

public class MultipartUtil {
    public static final String LINE_FEED = "\r\n";
    public static final String BOUNDARY = "------WebKitFormBoundaryUey8ljRiiZqhZHBu";

    private StringBuffer data = new StringBuffer();

    public void addFormField(String name, Object value) {
        data.append("--" + BOUNDARY + LINE_FEED);
        data.append("Content-Disposition: form-data; name=\"" + name + "\"");
        data.append(LINE_FEED + LINE_FEED);
        data.append(value + LINE_FEED);
    }

    public void addFilePart(String name, String value) throws IOException {
        File file = null;
        InputStream in = null;
        BufferedReader br = null;

        try {
            file = new File(value);
            data.append("--" + MultipartUtil.BOUNDARY + LINE_FEED);
            data.append("Content-Disposition: form-data; name=\"" + name + "\";filename=\"" + file.getName() + "\"" + LINE_FEED);
            // FIXME 文件类型
            data.append("Content-Type:image/jpeg" + LINE_FEED + LINE_FEED);

            in = new FileInputStream(file);
            br = new BufferedReader(new InputStreamReader(in, StandardCharsets.ISO_8859_1));
            String s = null;
            while ((s = br.readLine()) != null) {
                data.append(s);
            }
            data.append(LINE_FEED);
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public byte[] getData() {
        data.append(LINE_FEED + "--" + BOUNDARY + "--" + LINE_FEED);
        return data.toString().getBytes(StandardCharsets.UTF_8);
    }

    public int getLength() {
        return data.toString().getBytes(StandardCharsets.UTF_8).length + (LINE_FEED + "--" + BOUNDARY + "--" + LINE_FEED).length();
    }

    public static void main(String[] args) throws IOException {
        System.out.println(new File("D:\\1.jpg").length());

        InputStream in = new FileInputStream(new File("D:\\1.jpg"));
        int bytes = 0;
        byte[] bufferOut = new byte[1024];
        while ((bytes = in.read(bufferOut)) != -1) {
            for (int i = 0; i < bytes; i++) {
                System.out.print(bufferOut[i]);
            }
        }
        in.close();

        System.out.println("\n----------------------------------------------");

        StringBuffer data = new StringBuffer();
        in = null;
        BufferedReader br = null;
        in = new FileInputStream(new File("D:\\1.jpg"));
        br = new BufferedReader(new InputStreamReader(in, StandardCharsets.ISO_8859_1));
        String s = null;
        while ((s = br.readLine()) != null) {
            data.append(s);
        }
        br.close();

        for (byte b : data.toString().getBytes(StandardCharsets.ISO_8859_1)) {
            System.out.print(b);
        }

        System.out.println("----------------------------------------------");

        String str = new String(Files.readAllBytes(Paths.get("D:\\1.jpg")), StandardCharsets.UTF_8);

        for (byte b : str.getBytes(StandardCharsets.UTF_8)) {
            System.out.print(b);
        }
    }
}
